package com.niit.bc.servlet;

import com.niit.bc.bean.BookBean;
import com.niit.bc.bean.ListBean;
import com.niit.bc.biz.BookBiz;
import com.niit.bc.biz.ListBiz;
import com.niit.bc.biz.impl.BookBizImpl;
import com.niit.bc.biz.impl.ListBizImpl;
import com.niit.bc.util.web.Constants;
import com.niit.bc.util.web.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class BookList extends HttpServlet {


    private static final long serialVersionUID = 1L;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        response.setCharacterEncoding("utf8");
        request.setCharacterEncoding("utf8");

        /**
         * 左边图书分类列表
         * 分为大类型列表和小类型列表，其实现方式为找出大类型的id和name，通过大类型的id在小类型的表中查找
         * 相应的小类型名称，关键代码为<c:if test="${listType.typeId == listChildType.typeId }">
         */
        //列出所有类型的列表
        ListBiz listTypeBiz = new ListBizImpl();
        List <ListBean> listType = listTypeBiz.findListType();
        if (listType != null) {
            request.setAttribute("listType", listType);
        }
        //列出所有子类型的列表
        ListBiz listChildTypeBiz = new ListBizImpl();
        List <ListBean> listChildType = listChildTypeBiz.findListChildType();
        if (listChildType != null) {
            request.setAttribute("listChildType", listChildType);
        }

        /**
         * 左边图书的各种排行榜(取得bookId,bookName,bookAuthor,bookPhoto,bookPrice,bookPub,bookPubTime...)
         * 根据首页的需要来改变字段的选取，这样比全部选取book字段效率要高，此块查询语句可以在进行修改的
         * 一般为需要的图书基本信息
         */
        BookBiz bookBiz = new BookBizImpl();
        //推荐图书--点击数或者浏览量top10
        String panyu_sql1 = "SELECT bc_books.* FROM bc_books ORDER BY bookHits DESC LIMIT 0, 10;";
        List <BookBean> bookHitsList = bookBiz.findBookList(panyu_sql1);
        if (bookHitsList != null) {
            request.setAttribute("bookHitsList", bookHitsList);
        }

        //图书销售排行榜 --销售排行榜top10
        String panyu_sql2 = "SELECT bc_books.* FROM bc_books ORDER BY bookSale DESC LIMIT 0, 10;";
        List <BookBean> bookSaleList = bookBiz.findBookList(panyu_sql2);
        if (bookSaleList != null) {
            request.setAttribute("bookSaleList", bookSaleList);
        }
        //新书推荐 --上架时间top10
        String panyu_sql3 = "SELECT bc_books.* FROM bc_books ORDER BY bookStorageTime DESC LIMIT 0, 10;";
        List <BookBean> bookStorageTimeList = bookBiz.findBookList(panyu_sql3);
        if (bookStorageTimeList != null) {
            request.setAttribute("bookStorageTimeList", bookStorageTimeList);
        }


        int childTypeId = 0;
        int typeId = 0;
        int checkBy = 0;
        String sql = "";
        String bookListUrl = "";
        PageUtil pageUtil = new PageUtil(Constants.BOOKLIST_PAGESIZE, 0);
        //BookBizImpl bookBiz=new BookBizImpl();//上面已经定义过了
        List <BookBean> bookList = new ArrayList <BookBean>();

        String s_checkBy = request.getParameter("checkBy");
        if (s_checkBy != null && !s_checkBy.trim().equals("")) {
            checkBy = Integer.parseInt(s_checkBy);
        }

		/*
         * 获取页面的传递信息
		 */

        //获取子类编号
        String s_childTypeId = request.getParameter("childTypeId");
        if (s_childTypeId != null && !s_childTypeId.trim().equals("")) {
            childTypeId = Integer.parseInt(s_childTypeId);
            sql = "select bc_books.bookId,bc_books.bookName,bc_books.bookPrice," +
                    "bc_books.bookAuthor,bc_books.bookNum,bc_books.bookPhoto,bc_books.bookHits," +
                    "bc_books.bookStorageTime,bc_books.childTypeId,bc_books.bookSale" +
                    " from bc_books where childTypeId=" + childTypeId;
            request.setAttribute("list", "childTypeId=" + childTypeId);
            if (checkBy == 0) {
                bookListUrl = "BookList?childTypeId=" + childTypeId;
            }
            if (checkBy > 0) {
                bookListUrl = "BookList?childTypeId=" + childTypeId + "&checkBy=" + checkBy;
            }

        }
        //获取类编号
        String s_typeId = request.getParameter("typeId");
        if (s_typeId != null && !s_typeId.trim().equals("")) {
            typeId = Integer.parseInt(s_typeId);
            sql = "SELECT bc_books.bookId,bc_books.bookName,bc_books.bookPrice," +
                    "bc_books.bookAuthor,bc_books.bookNum,bc_books.bookPhoto,bc_books.bookHits," +
                    "bc_books.bookStorageTime,bc_books.childTypeId,bc_books.bookSale FROM " +
                    "bc_books left join bc_childtypes on  bc_childtypes.childTypeId = bc_books.childTypeId " +
                    " left join  bc_types  on  bc_types.typeId = bc_childtypes.typeId where bc_childTypes.typeId =" + typeId;
            request.setAttribute("list", "typeId=" + typeId);
            if (checkBy == 0) {
                bookListUrl = "BookList?typeId=" + typeId;
            }
            if (checkBy > 0) {
                bookListUrl = "BookList?typeId=" + typeId + "&checkBy=" + checkBy;
//System.out.println("ok");
            }
        }

        if (childTypeId == 0 && typeId == 0) {
            request.getRequestDispatcher("BookAllList").forward(request, response);
            return;
        }

        //获取查询条件信息

        if (checkBy == 0) {
            sql += " order by bc_books.bookStorageTime DESC";
        }
        if (checkBy == 1) {
            sql += " order by bc_books.bookPrice desc";
        }
        if (checkBy == 2) {
            sql += " order by bc_books.bookPrice asc";
        }
        if (checkBy == 3) {
            sql += " order by bc_books.bookStorageTime DESC";
        }


        //获取分页信息
        String spageCount = request.getParameter("pageCount");
        if (spageCount != null && !spageCount.trim().equals("")) {
            pageUtil.setPageCount(Integer.parseInt(spageCount));
        }

        String srowCount = request.getParameter("rowCount");
        if (srowCount != null && !srowCount.trim().equals("")) {
            pageUtil.setRowCount(Integer.parseInt(srowCount));
        }

        String spageCurr = request.getParameter("pageCurr");
        if (spageCurr != null && !spageCurr.trim().equals("")) {
            pageUtil.setPageCurr(Integer.parseInt(spageCurr));
        }
//System.out.println("sql语句是:"+sql);
//System.out.println("bookListUrl路径是："+bookListUrl);
        if (!sql.trim().equals("")) {
            bookList = bookBiz.findBookListByInfo(pageUtil, sql);
            if (bookList != null) {
                request.setAttribute("bookListUrl", bookListUrl);
                request.setAttribute(Constants.BOOKLIST, bookList);
                request.setAttribute("pageUtil", pageUtil);
                request.getRequestDispatcher("bookList.jsp").forward(request, response);
            }
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        doGet(request, response);
    }

}
